/**
 * @summary plug
 */
 import * as http from 'http';
import { Server, Socket } from 'socket.io';
import { DefaultEventsMap } from 'socket.io/dist/typed-events';

/**
 * @class socket.io
 * @link https://socket.io/zh-CN/docs/v4/emitting-events/
 */
class SocketIo {
    public static connection(_server:  http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>): void {
        const io = new Server(_server);
        io.on('connection', socket => {
            console.log('用户socket链接成功');
            socket.on('click1', (e) => {
                console.log('点击事件', e);
            })

            this.onEmit(socket);
            this.AllUserBroadcast(io);
            this.userDisconnect(socket);
        });
    }
    private static userDisconnect(_socket: Socket<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>): void {
        _socket.on('disconnect', () => {
            console.log('用户断开链接');
        })
    }
    private static AllUserBroadcast(_io: Server<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>): void {
        _io.emit('all', '发送所有用户');
    }
    private static onEmit(_socket: Socket<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>): void {
        _socket.emit('click2', {datas: 'datas'});
    }
}

export default SocketIo;